<?php
/**
 * Created by PhpStorm.
 * User: wangwenshan
 * Date: 2019/1/21
 * Time: 15:18
 */

namespace app\api\model;


class Product extends  BaseModel
{
    protected $hidden = ['delete_time','update_time','create_time','category_id','from','pivot'];
    public function getMainImgUrlAttr($value,$data){
        return $this->prefixImgUrl($value,$data);
    }
    public function imgs(){
        return $this->hasMany('ProductImage','product_id','id');
    }
    public function properties(){
        return $this->hasMany('ProductProperty','product_id','id');
    }
    public static function getMostRecent($count){
        $products = self::limit($count)->order('create_time desc')->select();
        return $products;
    }
    public static function getProductsByCategoryID($categoryID){
        $products = self::where('category_id','=',$categoryID)->select();
        return $products;
    }
    public static function getProductDetail($id){
        $product = self::with([
            'imgs' => function($query){//闭包函数
                $query->with(['imgUrl'])
                    ->order('order','asc');
            }
        ])->with(['properties'])->find($id);//链式方法的with可以连接多个
        return $product;
    }
}